/* eslint-disable */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { ModalC, FormGroup, DATA_ENTRY } from '@/src/components';
import moment from 'moment';
import _ from 'lodash';

const FormItemLayout = {
  labelCol: {
    span: 6,
    offset: 0,
  },
  wrapperCol: {
    span: 18,
    offset: 0,
  },
};
const FormItemLayout1 = {
  labelCol: {
    span: 8,
    offset: 0,
  },
  wrapperCol: {
    span: 16,
    offset: 0,
  },
};
const FormItemLayout2 = {
  labelCol: {
    span: 9,
    offset: 0,
  },
  wrapperCol: {
    span: 15,
    offset: 0,
  },
};
class ModalRenderC extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {};
    this.FormGroupReftraders = React.createRef();
    this.FormGroupRefrate = React.createRef();
    this.FormGroupRefcontrollingShareholder = React.createRef();
    this.FormGroupRefotherShareholder = React.createRef();
    this.FormGroupRefbeneficiary = React.createRef();
    this.FormGroupRefrepresentative = React.createRef();
  }

  componentDidMount() {
    // 父组件对本组件的引用
    // this.props.onRef(this);
  }

  dateChange = (e, field) => {
    this.setState({
      [field]: e,
    });
  };

  // 开始时间小于结束时间
  disabledStartDate = (startValue, field) => {
    const dateFormat = 'YYYY-MM-DD';
    if (!startValue || !this.state[field]) {
      return false;
    }
    return startValue.format(dateFormat) > this.state[field].format(dateFormat);
  };

  // 结束时间大于开始时间
  disabledEndDate = (endValue, field) => {
    // const { startDate } = this.state;
    if (!endValue || !this.state[field]) {
      return false;
    }
    return endValue < this.state[field];
  };

  formRender() {
    const {
      type,
      globalM: {
        dataDictionary: { COUNTER_PARTY_RATE_ORGAN = [], COUNTER_PARTY_CERTIFICATE_TYPE = [], COUNTER_PARTY_RATE_TYPE = [] },
      },
    } = this.props;
    return (
      <div>
        {type == 'traders' ? (
          <FormGroup
            onRef={(ref) => (this.FormGroupReftraders.current = ref)}
            autoLayout={false}
            rowNumber={1}
            dataSource={[
              {
                dataIndex: 'traderCode',
                type: DATA_ENTRY.INPUT,
                allowClear: true,
                title: '交易员代码',
                initialValue: undefined,
                formItemLayout: FormItemLayout,
              },
              {
                dataIndex: 'traderName',
                type: DATA_ENTRY.INPUT,
                allowClear: true,
                title: '交易员名称',
                initialValue: undefined,
                rules: [{ required: true, message: '请填写交易员名称' }],
                formItemLayout: FormItemLayout,
              },
            ]}
          />
        ) : null}
        {type == 'rate' ? (
          <FormGroup
            onRef={(ref) => (this.FormGroupRefrate.current = ref)}
            autoLayout={false}
            rowNumber={1}
            dataSource={[
              {
                dataIndex: 'ratingAgency',
                type: DATA_ENTRY.SELECT,
                allowClear: true,
                title: '评级机构',
                dataSource: COUNTER_PARTY_RATE_ORGAN?.map((item) => {
                  return { title: item.value, value: item.code };
                }),
                initialValue: undefined,
                rules: [{ required: true, message: '请选择评级机构' }],
                formItemLayout: FormItemLayout,
                positionBody: true,
              },
              {
                dataIndex: 'rating',
                type: DATA_ENTRY.INPUT,
                title: '评级',
                initialValue: undefined,
                rules: [{ required: true, message: '请填写评级' }],
                formItemLayout: FormItemLayout,
              },
              {
                dataIndex: 'ratingType',
                type: DATA_ENTRY.SELECT,
                allowClear: true,
                filterOption: false,
                title: '评级类型',
                dataSource: COUNTER_PARTY_RATE_TYPE.map((item) => {
                  return { title: item.value, value: item.code };
                }),
                initialValue: undefined,
                rules: [{ required: true, message: '请选择评级类型' }],
                formItemLayout: FormItemLayout,
              },
              {
                dataIndex: 'ratingDate',
                type: DATA_ENTRY.SINGLEDATE,
                title: '评级日期',
                initialValue: undefined,
                rules: [{ required: true, message: '请选择评级日期' }],
                formItemLayout: FormItemLayout,
              },
            ]}
          />
        ) : null}
        {type == 'controllingShareholder' ? (
          <FormGroup
            onRef={(ref) => (this.FormGroupRefcontrollingShareholder.current = ref)}
            autoLayout={false}
            rowNumber={1}
            dataSource={[
              {
                dataIndex: 'nameOfShareholder',
                type: DATA_ENTRY.INPUT,
                title: '控股股东名称',
                initialValue: undefined,
                rules: [{ required: true, message: '请填写控股股东名称' }],
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'certificateType',
                type: DATA_ENTRY.SELECT,
                allowClear: true,
                filterOption: false,
                title: '控股股东证件类型',
                dataSource: COUNTER_PARTY_CERTIFICATE_TYPE.map((item) => {
                  return { title: item.value, value: item.code };
                }),
                initialValue: undefined,
                rules: [{ required: true, message: '请选择控股股东证件类型' }],
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'identificationNumber',
                type: DATA_ENTRY.INPUT,
                title: '控股股东证件号码',
                initialValue: undefined,
                rules: [{ required: true, message: '请填写控股股东证件号码' }],
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'shareholdingRatio',
                type: DATA_ENTRY.INPUT,
                title: '控股股东持股比例(%)',
                initialValue: undefined,
                rules: [{ pattern: /^([1-9]?\d(\.\d\d?\d?)?)$|100$/, message: '请输入0-100的百分数' }],
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'effectiveDate',
                type: DATA_ENTRY.SINGLEDATE,
                title: '有效期起始日',
                initialValue: undefined,
                onChange: (e) => {
                  this.dateChange(e, 'effectiveDate');
                },
                rules: [{ required: true, message: '请选择有效期起始日' }],
                formItemLayout: FormItemLayout1,
                // disabledDate={(e) => this.disabledStartDate(e, 'expiryDate')}
              },
              {
                dataIndex: 'expiryDate',
                type: DATA_ENTRY.SINGLEDATE,
                title: '有效期截止日',
                initialValue: undefined,
                onChange: (e) => {
                  this.dateChange(e, 'expiryDate');
                },
                rules: [{ required: true, message: '请选择评级日期' }],
                formItemLayout: FormItemLayout1,
                // disabledDate={(e) => this.disabledEndDate(e, 'effectiveDate')}
              },
            ]}
          />
        ) : null}
        {type === 'otherShareholder' ? (
          <FormGroup
            onRef={(ref) => (this.FormGroupRefotherShareholder.current = ref)}
            autoLayout={false}
            rowNumber={1}
            dataSource={[
              {
                dataIndex: 'nameOfShareholder',
                type: DATA_ENTRY.INPUT,
                title: '其他股东名称',
                initialValue: undefined,
                rules: [{ required: true, message: '请填写控股股东名称' }],
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'certificateType',
                type: DATA_ENTRY.SELECT,
                allowClear: true,
                title: '其他股东证件类型',
                dataSource: COUNTER_PARTY_CERTIFICATE_TYPE.map((item) => {
                  return { title: item.value, value: item.code };
                }),
                initialValue: undefined,
                rules: [{ required: true, message: '请选择其他股东证件类型' }],
                filterOption: false,
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'identificationNumber',
                type: DATA_ENTRY.INPUT,
                title: '其他股东证件号码',
                initialValue: undefined,
                rules: [{ required: true, message: '请填写其他股东证件号码' }],
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'shareholdingRatio',
                type: DATA_ENTRY.INPUT,
                title: '其他股东持股比例(%)',
                initialValue: undefined,
                rules: [{ pattern: /^([1-9]?\d(\.\d\d?\d?)?)$|100$/, message: '请输入0-100的百分数' }],
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'effectiveDate',
                type: DATA_ENTRY.SINGLEDATE,
                title: '有效期起始日',
                initialValue: undefined,
                onChange: (e) => {
                  this.dateChange(e, 'effectiveDate');
                },
                rules: [{ required: true, message: '请选择有效期起始日' }],
                formItemLayout: FormItemLayout1,
                // disabledDate={(e) => this.disabledStartDate(e, 'expiryDate')}
              },
              {
                dataIndex: 'expiryDate',
                type: DATA_ENTRY.SINGLEDATE,
                title: '有效期截止日',
                initialValue: undefined,
                onChange: (e) => {
                  this.dateChange(e, 'expiryDate');
                },
                rules: [{ required: true, message: '请选择评级日期' }],
                formItemLayout: FormItemLayout1,
                // disabledDate={(e) => this.disabledEndDate(e, 'effectiveDate')}
              },
            ]}
          />
        ) : null}
        {type === 'beneficiary' ? (
          <FormGroup
            onRef={(ref) => (this.FormGroupRefbeneficiary.current = ref)}
            autoLayout={false}
            rowNumber={1}
            dataSource={[
              {
                dataIndex: 'nameOfShareholder',
                type: DATA_ENTRY.INPUT,
                title: '受益所有人名称',
                initialValue: undefined,
                rules: [{ required: true, message: '请填写受益所有人名称' }],
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'certificateType',
                type: DATA_ENTRY.SELECT,
                allowClear: true,
                title: '受益人证件类型',
                dataSource: COUNTER_PARTY_CERTIFICATE_TYPE.map((item) => {
                  return { title: item.value, value: item.code };
                }),
                initialValue: undefined,
                rules: [{ required: true, message: '请选择受益人证件类型' }],
                filterOption: false,
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'identificationNumber',
                type: DATA_ENTRY.INPUT,
                title: '受益人证件号码',
                initialValue: undefined,
                rules: [{ required: true, message: '请填写受益人证件号码' }],
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'address',
                type: DATA_ENTRY.INPUT,
                title: '受益人地址',
                initialValue: undefined,
                formItemLayout: FormItemLayout1,
              },
              {
                dataIndex: 'effectiveDate',
                type: DATA_ENTRY.SINGLEDATE,
                title: '有效期起始日',
                initialValue: undefined,
                onChange: (e) => {
                  this.dateChange(e, 'effectiveDate');
                },
                rules: [{ required: true, message: '请选择有效期起始日' }],
                formItemLayout: FormItemLayout1,
                // disabledDate={(e) => this.disabledStartDate(e, 'expiryDate')}
              },
              {
                dataIndex: 'expiryDate',
                type: DATA_ENTRY.SINGLEDATE,
                title: '有效期截止日',
                initialValue: undefined,
                onChange: (e) => {
                  this.dateChange(e, 'expiryDate');
                },
                rules: [{ required: true, message: '请选择评级日期' }],
                formItemLayout: FormItemLayout1,
                // disabledDate={(e) => this.disabledEndDate(e, 'effectiveDate')}
              },
            ]}
          />
        ) : null}
        {type === 'representative' ? (
          <FormGroup
            onRef={(ref) => (this.FormGroupRefrepresentative.current = ref)}
            autoLayout={false}
            rowNumber={1}
            dataSource={[
              {
                dataIndex: 'nameOfShareholder',
                type: DATA_ENTRY.INPUT,
                title: '法定代表人名称',
                initialValue: undefined,
                rules: [{ required: true, message: '请填写法定代表人名称' }],
                formItemLayout: FormItemLayout2,
              },
              {
                dataIndex: 'certificateType',
                type: DATA_ENTRY.SELECT,
                allowClear: true,
                title: '法定代表人证件类型',
                dataSource: COUNTER_PARTY_CERTIFICATE_TYPE.map((item) => {
                  return { title: item.value, value: item.code };
                }),
                initialValue: undefined,
                rules: [{ required: true, message: '请选择法定代表人证件类型' }],
                filterOption: false,
                formItemLayout: FormItemLayout2,
              },
              {
                dataIndex: 'identificationNumber',
                type: DATA_ENTRY.INPUT,
                title: '法定代表人证件号码',
                initialValue: undefined,
                rules: [{ required: true, message: '请填写法定代表人证件号码' }],
                formItemLayout: FormItemLayout2,
              },
              {
                dataIndex: 'effectiveDate',
                type: DATA_ENTRY.SINGLEDATE,
                title: '有效期起始日',
                initialValue: undefined,
                onChange: (e) => {
                  this.dateChange(e, 'effectiveDate');
                },
                rules: [{ required: true, message: '请选择有效期起始日' }],
                formItemLayout: FormItemLayout2,
                // disabledDate={(e) => this.disabledStartDate(e, 'expiryDate')}
              },
              {
                dataIndex: 'expiryDate',
                type: DATA_ENTRY.SINGLEDATE,
                title: '有效期截止日',
                initialValue: undefined,
                onChange: (e) => {
                  this.dateChange(e, 'expiryDate');
                },
                rules: [{ required: true, message: '请选择评级日期' }],
                formItemLayout: FormItemLayout2,
                // disabledDate={(e) => this.disabledEndDate(e, 'effectiveDate')}
              },
            ]}
          />
        ) : null}
      </div>
    );
  }

  handleOk = async () => {
    const { type } = this.props;
    const { params } = this.props.that.state;
    const a = this.FormGroupReftraders.current ? await this.FormGroupReftraders.current?.getFormGroupData() : {};
    const b = this.FormGroupRefrate.current ? await this.FormGroupRefrate.current?.getFormGroupData() : {};
    const c = this.FormGroupRefcontrollingShareholder.current ? await this.FormGroupRefcontrollingShareholder.current?.getFormGroupData() : {};
    const d = this.FormGroupRefotherShareholder.current ? await this.FormGroupRefotherShareholder.current?.getFormGroupData() : {};
    const e = this.FormGroupRefbeneficiary.current ? await this.FormGroupRefbeneficiary.current?.getFormGroupData() : {};
    const f = this.FormGroupRefrepresentative.current ? await this.FormGroupRefrepresentative.current?.getFormGroupData() : {};
    const fieldsValue = { ...a, ...b, ...c, ...d, ...e, ...f };
    if (fieldsValue.ratingDate != undefined) {
      fieldsValue.ratingDate = moment(fieldsValue.ratingDate).format('YYYY-MM-DD');
    }
    if (fieldsValue.effectiveDate != undefined) {
      fieldsValue.effectiveDate = moment(fieldsValue.effectiveDate).format('YYYY-MM-DD');
      fieldsValue.expiryDate = moment(fieldsValue.expiryDate).format('YYYY-MM-DD');
    }
    const timestamp = new Date().getTime(); // 添加时间戳作为唯一标识
    if (!Object.keys(params).length) return;
    if (type == 'traders' || type == 'rate') {
      if (!params.counterparties[type].dataSource) {
        params.counterparties[type].dataSource = [];
      }
      params.counterparties[type].dataSource.push({ ...fieldsValue, key: timestamp });
    } else {
      if (!params.organ[type].dataSource) {
        params.organ[type].dataSource = [];
      }
      params.organ[type].dataSource.push({ ...fieldsValue, key: timestamp });
    }
    this.props.that.setState({
      params: _.cloneDeep(params),
    });
    this.handleCancel();
  };

  handleCancel = () => {
    const { that } = this.props;
    this.FormGroupReftraders.current?.formRef && this.FormGroupReftraders.current?.formRef.current?.resetFields();
    this.FormGroupRefrate.current?.formRef && this.FormGroupRefrate.current?.formRef.current?.resetFields();
    this.FormGroupRefcontrollingShareholder.current?.formRef && this.FormGroupRefcontrollingShareholder?.current.formRef.current?.resetFields();
    this.FormGroupRefotherShareholder.current?.formRef && this.FormGroupRefotherShareholder.current?.formRef.current?.resetFields();
    this.FormGroupRefbeneficiary.current?.formRef && this.FormGroupRefbeneficiary.current?.formRef.current?.resetFields();
    this.FormGroupRefrepresentative.current?.formRef && this.FormGroupRefrepresentative.current?.formRef.current?.resetFields();
    // 取消后应该卸载ref，否则下次打开会找不到dom
    this.FormGroupReftraders.current = undefined;
    this.FormGroupRefrate.current = undefined;
    this.FormGroupRefcontrollingShareholder.current = undefined;
    this.FormGroupRefotherShareholder.current = undefined;
    this.FormGroupRefbeneficiary.current = undefined;
    this.FormGroupRefrepresentative.current = undefined;
    that.setState({
      visible: false,
    });
    this.setState({
      effectiveDate: '',
      expiryDate: '',
    });
  };

  render() {
    const { title, visible } = this.props;
    return (
      <div>
        <ModalC
          title={title}
          visible={visible}
          onOk={this.handleOk}
          onCancel={this.handleCancel}
          width={452}
          maskStyle={{ background: 'rgba(0,0,0,0.4)' }}
          wrapClassName='counterpartyModal'
        >
          <div>{this.formRender()}</div>
        </ModalC>
      </div>
    );
  }
}

export default connect(({ counterpartyInformationM, globalM, loading }) => ({
  counterpartyInformationM,
  globalM,
  loading,
}))(ModalRenderC);
